package com.yanchangapp.ui.activity.purchase.record;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanchangapp.R;
import com.yanchangapp.adapter.fragment.ContractListFgAdapter;
import com.yanchangapp.adapter.fragment.RecordListFgAdapter;
import com.yanchangapp.commom.RecordStatus;
import com.yanchangapp.model.bean.TabBean;
import com.yanchangapp.ui.fragment.purchase.ContractListFragment;
import com.yanchangapp.ui.fragment.purchase.GoodsRecordFragment;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.util.ui.ResHelper;
import com.zxl.baselib.widget.NiceViewPagerIndicator;
import com.zxl.baselib.widget.ViewPagerFixed;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * 采购记录活动
 */
public class GoodsRecordActivity extends BaseActivity{

    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.niceIndicator)
    NiceViewPagerIndicator mIndicator;
    @BindView(R.id.viewPager)
    ViewPagerFixed viewPager;

    private int currentSelection = 0;
    // 标题栏目
    private List<TabBean> mTabBeanList;
    // 列表
    private List<GoodsRecordFragment> mListFragments;

    public static String REFRESH_RECORD_LIST = "REFRESH_RECORD_LIST";

    @Override
    protected void init() {
        // 初始化并加载tab数据
        mTabBeanList = new ArrayList<>();
        mTabBeanList.add(new TabBean(RecordStatus.WAIT_APPROVE.getName(),RecordStatus.WAIT_APPROVE.getStatus()));
        mTabBeanList.add(new TabBean(RecordStatus.HAS_PASS.getName(),RecordStatus.HAS_PASS.getStatus()));
        mTabBeanList.add(new TabBean(RecordStatus.HAS_REJECT.getName(),RecordStatus.HAS_REJECT.getStatus()));
        mListFragments = new ArrayList<>();
    }

    // 采购记录页面的入口
    public static void show(Activity activity) {
        Intent intent = new Intent(activity, GoodsRecordActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_goods_record;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    private GoodsRecordFragment createGoodsRecordFragment(TabBean tabBean){
        GoodsRecordFragment goodsRecordFragment = new GoodsRecordFragment();
        Bundle bundle = new Bundle();
        bundle.putString(GoodsRecordFragment.TITLE,tabBean.getTitle());
        bundle.putString(GoodsRecordFragment.VALUE,tabBean.getParams());
        goodsRecordFragment.setArguments(bundle);
        return goodsRecordFragment;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        //设置标题
        tvToolbarTitle.setText("采购记录");

        // 设置fragment信息
        for (int i=0;i<mTabBeanList.size();i++){
            mListFragments.add(createGoodsRecordFragment(mTabBeanList.get(i)));
        }

        RecordListFgAdapter recordListFgAdapter =
                new RecordListFgAdapter(getSupportFragmentManager(), mListFragments, mTabBeanList);
        viewPager.setAdapter(recordListFgAdapter);
        viewPager.setCurrentItem(0);
        // 设置页面缓存数量...
        viewPager.setOffscreenPageLimit(mListFragments.size());
        mIndicator.setIndicatorLengthType(NiceViewPagerIndicator.IndicatorType.ABSOLUTE_LENGTH)
                .setIndicatorLength(40)
                .setIsExpand(true)
                .setSelectedTextColor(ResHelper.getColor(R.color.colorPrimary))
                .setIndicatorColor(ResHelper.getColor(R.color.colorPrimary));
        mIndicator.setUpViewPager(viewPager);
    }

    @Override
    protected void initData() {
        mRxManager.on(REFRESH_RECORD_LIST, (Consumer<Boolean>) aBoolean -> justRefresh());
    }

    /**
     * 模拟刷新
     */
    private void justRefresh() {
        GoodsRecordFragment fragment = mListFragments.get(currentSelection);
        fragment.handleRefresh();
    }

    @Override
    protected void initListener() {
        // 点击返回键 进行回退处理
        ivToolbarNavigation.setOnClickListener(v -> finish());

        // 监听页面切换
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                // 当前选中哪个Fragment....
                currentSelection = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

}
